package org.snowin.common;

import dao.SmsDao;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.List;



public abstract class BaseCase extends BaseAction
{
    protected String result = "pass";
    protected String description = "Default Description";
    protected String message = "Default message";
    protected String caseName;
    protected String logFolder;

    public abstract void execute()
            throws Exception;

    public void initialSessionData(SessionData sessionData)
    {
        this.sessionData = sessionData;
        this.wtdGlobal = new WTDGlobal();
        this.webDriver = sessionData.getWebDriver();
        this.hostName = sessionData.getHostName();
        this.logger = sessionData.getLogger();
    }

    public SessionData getSessionData()
    {
        return this.sessionData;
    }

    public void setSessionData(SessionData sessionData)
    {
        this.sessionData = sessionData;
        this.webDriver = sessionData.getWebDriver();
        this.hostName = sessionData.getHostName();
        this.logger = sessionData.getLogger();
    }

    public String getTestResult()
    {
        return this.result;
    }

    public String getDescription()
    {
        return this.description;
    }

    public void setDescription(String description)
    {
        this.description = description;
    }

    public void setTestResult(String result)
    {
        this.result = result;
    }

    public void setErrorInfo(String message)
    {
        this.message = message;
    }

    public String getErrorInfo()
    {
        return this.message;
    }

    public String sendSms(String phoneNum, String inputId, String clickBtId)
    {
        SmsDao smD = new SmsDao(this.sessionData);
        String authCod = "";
        long beforeExecute = smD.getLatestId();
        if (!sendKeysById(inputId, phoneNum)) {
            this.logger.error("input the phone number failed");
        }
        if (!clickById(clickBtId)) {
            this.logger.error("click the send message button failed");
        }
        ResultSet rs = smD.getLatestRecord();

        int j = 0;
        while (j < 30) {
            try
            {
                if (beforeExecute < Long.parseLong(rs.getString("iAutoID"))) {
                    for (int i = 0; i <= Long.parseLong(rs.getString("iAutoID")) - beforeExecute; i++)
                    {
                        ResultSet rs1 = smD.executeSql("select * from  t_sms where iAutoID=" + beforeExecute + i + 1);
                        if (rs1.getString("sCellPhone").equalsIgnoreCase(phoneNum))
                        {
                            String message = rs1.getString("sMessage");

                            authCod = message.substring(message.indexOf("��֤���ǣ�"), message.indexOf("��֤���ǣ�") + 6);
                            break;
                        }
                    }
                }
                Thread.sleep(2000L);
                j++;
            }
            catch (Exception e)
            {
                this.logger.info("Exception", e);
            }
        }
        smD.closeConnection();
        return authCod;
    }

    public String getSms(String phoneNum)
    {
        SmsDao smD = new SmsDao(this.sessionData);
        String authCod = "";
        long beforeExecute = smD.getLatestId();

        ResultSet rs = smD.getLatestRecord();

        int j = 0;
        while (j < 30)
        {
            j++;
            try
            {
                if (beforeExecute < Long.parseLong(rs.getString("iAutoID"))) {
                    for (int i = 0; i <= Long.parseLong(rs.getString("iAutoID")) - beforeExecute; i++)
                    {
                        ResultSet rs1 = smD.executeSql("select * from  t_sms where iAutoID=" + beforeExecute + i + 1);
                        if (rs1.getString("sCellPhone").equalsIgnoreCase(phoneNum))
                        {
                            String message = rs1.getString("sMessage");

                            authCod = message.substring(message.indexOf("��֤���ǣ�"), message.indexOf("��֤���ǣ�") + 6);
                            break;
                        }
                    }
                }
                Thread.sleep(2000L);
            }
            catch (Exception e)
            {
                this.logger.info("Exception", e);
            }
        }
        smD.closeConnection();
        return authCod;
    }

    protected void setPointResult(String name, String description, String result, String errorInfo)
    {
        String resultFile = this.sessionData.getLogFolder() + System.getProperty("file.separator") + "result.xml";

        File  rFile =new File(resultFile);
        if (!rFile.exists()){
            try {
                rFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        String temString = " {" + errorInfo + "}";


        this.sessionData.getLogger().info("~~~~~~ Test Point 【 " + name + " 】  : (" + description + ")  is 【 " + result + " 】 " + temString);
        try
        {
            SAXReader reader = new SAXReader();
            Document document = reader.read(new File(resultFile));
            Element root = document.getRootElement();
            List<Element> elements = document.selectNodes("//TestCase[@name='" + this.sessionData.getCaseName() + "']");
            for (Element el : elements)
            {
                Element temEl = el.addElement("TestPoint");
                temEl.addAttribute("name", name);
                temEl.addAttribute("caseType", "common");
                temEl.addAttribute("result", result);
                temEl.addAttribute("description", description);
                temEl.addAttribute("errorInfo", errorInfo);
            }
            OutputFormat outFmt = new OutputFormat("\t", true, "UTF-8");
            XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(resultFile));

            xmlWriter.write(document);
            xmlWriter.close();
        }
        catch (Exception e)
        {
            this.sessionData.getLogger().error(" ****** Modify Result XML Failed ******** ");

            this.sessionData.getLogger().error("errorInfo", e);
        }
    }
}
